import axios from "axios";
import {Message} from 'element-ui'
// 数据仓库

export const instance = axios.create({
    timeout: 1000 * 30,
    baseURL: 'http://192.168.3.10:8080',
    //baseURL: process.env.VUE_APP_BASE_URL_SHIP,
});

instance.interceptors.request.use(
    function (config) {
        // config.headers["authorization"] ="Bearer eyJhbGciOiJIUzUxMiJ9.eyJ1c2VySWQiOjEyNjU0NzY4OTA2NzI2NzI4MDgsImFjY291bnQiOiJCRkJIcGdObVJRMmhuNCtIeERTUFJBPT0iLCJ1dWlkIjoiZTIzNmUxYzctZmM3Ni00MzA5LWJjMjktZWQxNmM0M2NlMmFlIiwic3ViIjoiMTI2NTQ3Njg5MDY3MjY3MjgwOCIsImlhdCI6MTcyMjQ5NTA2MywiZXhwIjoxNzIyNTgxNDYzfQ.RyTphg2ShKnSMT1AiUPMFvSqRKoia7z0rxATaQVk-RQoIsZAvuTdj8R-AmI6UvP8RfhdDxPuaMmJHhaSxMXj1w"

        config.headers["authorization"] =
            "Bearer " + localStorage.TOKEN?.replace(/\"/g, "");
        // config.headers['Content-Type']='multipart/form-data'
        return config;
    },
    function (error) {
        return Promise.reject(error);
    }
);

instance.interceptors.response.use(
    function (response) {
        if (response.data.code == 200){
            return response.data.data;
        } 
        throw new Error();
    },
    function (error) {
        let { message } = error;
        if (message == "Network Error") {
          message = "后端接口连接异常";
        } else if (message.includes("timeout")) {
          message = "系统接口请求超时";
        } else if (message.includes("Request failed with status code")) {
          message = "系统接口" + message.substr(message.length - 3) + "异常";
        }
        Message({ message: message, type: 'error', duration: 5 * 1000 })

        return Promise.reject(error);
    }
);

